package gui.dialog;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;

import game.PlayerColor;
import main.Antiquity;

/**
 *	@(#)OptionsDialog.java
 *
 *	The <code>OptionsDialog</code> class is used to create a dialog in which
 *	you can change the names and colors of the players.
 *
 *	@author Grzegorz Kobiela
 *	@version 1.0 2007/9/11
 */
public final class OptionsDialog
extends JDialog
{
	/**
	 *	Creates a new <code>OptionsDialog</code> instance.
	 *	@param window the game window
	 */
	public OptionsDialog(final gui.GameWindow window) {
		super(window, Antiquity.get("dlgOptionsTitle"), true);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		Container contentPane = getContentPane();
		contentPane.setLayout(new FlowLayout());

		/* Creates the main panel. */
		JPanel panel = new JPanel(new BorderLayout(10, 10));
		panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), Antiquity.get("dlgOptionsBorderTitle")));
		contentPane.add(panel);

		/* Creates the labels. */
		JPanel inner = new JPanel(new GridLayout(4, 1, 5, 5));
		ArrayList<String> names = Antiquity.getNames();
		for(int i = 1; i <= names.size(); i++) inner.add(new JLabel(Antiquity.get("dlgOptionsLabel" + i) + ":"));
		panel.add(inner, BorderLayout.WEST);

		/* Creates the textfields. */
		inner = new JPanel(new GridLayout(4, 1, 5, 5));
		final ArrayList<JTextField> textFields = new ArrayList<JTextField>();
		for(String name: names) {
			JTextField textField = new JTextField(name, 20);
			textFields.add(textField);
			inner.add(textField);
		}
		panel.add(inner, BorderLayout.CENTER);

		/* Creates the comboboxes. */
		inner = new JPanel(new GridLayout(4, 1, 5, 5));
		ArrayList<PlayerColor> colors = Antiquity.getColors();
		ArrayList<ImageIcon> icons = new ArrayList<ImageIcon>();
		for(PlayerColor color: colors) {
			ImageIcon icon = gui.GameUtilities.createIcon(color);
			icon.setDescription(color.toString().toUpperCase());
			icons.add(icon);
		}
		final ArrayList<JComboBox> comboBoxes = new ArrayList<JComboBox>();
		for(PlayerColor color: colors) {
			JComboBox comboBox = new JComboBox(icons.toArray());
			comboBox.setEditable(false);
			comboBox.setSelectedIndex(color.ordinal() - 1);
			comboBoxes.add(comboBox);
			inner.add(comboBox);
		}
		panel.add(inner, BorderLayout.EAST);

		/* Creates the 'OK' button. */
		contentPane.add(panel = new JPanel(new BorderLayout(10, 10)));
		JButton button = new JButton(Antiquity.get("btnOK"));
		button.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int maxLength = (Integer)Antiquity.getProperty("OptionsDialogMaxLength");

					/* Check names */
					ArrayList<String> names = new ArrayList<String>();
					for(JTextField textField: textFields) names.add(textField.getText());
					for(String name: names) {
						if(name.equals("")) {
							window.showError((JButton)e.getSource(), Antiquity.get("errNoName"));
							return;
						} else if(name.length() > maxLength) {
							window.showError((JButton)e.getSource(), String.format(Antiquity.get("errTooLong"), maxLength));
							return;
						}
					}

					/* Check colors */
					int sameColor = 0;
					ArrayList<PlayerColor> colors = new ArrayList<PlayerColor>();
					for(JComboBox comboBox: comboBoxes) colors.add(Enum.valueOf(PlayerColor.class, ((ImageIcon)comboBox.getSelectedItem()).getDescription()));
					for(PlayerColor color: colors) sameColor |= 1 << color.ordinal() - 1;
					if(sameColor != (1 << colors.size()) - 1) {
						window.showError((JButton)e.getSource(), Antiquity.get("errSameColor"));
						return;
					}

					/* Save names and colors */
					Antiquity.setNamesAndColors(names, colors);
					dispose();
				}
			}
		);
		panel.add(button, BorderLayout.WEST);

		/* Creates the 'Cancel' button. */
		button = new JButton(Antiquity.get("btnCancel"));
		button.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					dispose();
				}
			}
		);
		panel.add(button, BorderLayout.EAST);

		/* Window properties */
		Rectangle bounds = (Rectangle)Antiquity.getProperty("OptionsDialogBounds");
		setLocation(
			window.getX() + (window.getWidth() - bounds.width) / 2,
			window.getY() + (window.getHeight() - bounds.height) / 2
		);
		setSize(bounds.getSize());
		setResizable(false);
		setVisible(true);
	}
}